import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';

import { fetchValidation } from '@/utils';
import { queryDeliveryDetail } from './service';

export type Effect = (
    action: AnyAction,
    effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
    namespace: string;
    state: {};
    effects: {
        fetch: Effect;
    };
    reducers: {
        show: Reducer;
    };
}

const Model: ModelType = {
    namespace: 'logisCenterUnmannedDetail',

    state: {
        basicGoods: [],
    },

    effects: {
        * fetch({ payload }, { call, put }) {
            const response = yield call(queryDeliveryDetail, payload);
            const data = fetchValidation(response);
            yield data && put({
                type: 'show',
                payload: response.body,
            });
        },
    },

    reducers: {
        show(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
    },
};

export default Model;
